package com.automapper.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.automapper.constants.ParameterConstants;
import com.automapper.exception.AutoMapperException;

public class ParameterUtils {
	
	private ParameterUtils(){}

	public static List<Object> getParameterList(Map<String, Object> parameterMap) {
		List<Object> list;
		Object paramObj = parameterMap.get(ParameterConstants.PARAM);
		
		AutoMapperException.NullRuntimeException(paramObj, ParameterConstants.PARAM + "对象不能为空");
		
		if (paramObj instanceof List) {
			list = (List<Object>) paramObj;
			AutoMapperException.CollectionIsBlankRuntimeException(list, "List对象不能为空集合");
		} else {
			//将非集合对象封装成集合对象
			list = new ArrayList<>();
			list.add(paramObj);
		}
		parameterMap.put(ParameterConstants.PARAM, list);
		return list;
	}
	
	/**
	 * 获取需要入库的参数
	 * @param parameterMap
	 * @return
	 */
	public static Object getParameter(Map<String, Object> parameterMap) {
		Object paramObj = parameterMap.get(ParameterConstants.PARAM);
		AutoMapperException.NullRuntimeException(paramObj, ParameterConstants.PARAM + "对象不能为空");
		return paramObj;
	}
	
	public static boolean isAutoMapperParameter(Object parameter) {
		// 在xml的insert，update，delete，select标签定义parameterType后，mybatis不会把参数转换成Map对象
		// 所以在这里判断parameter是否Map对象
		if (!(parameter instanceof Map)) {
			return false;
		}
		
		Map<String, Object> parameterMap = (Map<String, Object>) parameter;
		if(!parameterMap.containsKey(ParameterConstants.PARAM)) {
			return false;
		}
		return true;
	}
}
